package com.atguigu.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.base.BaseDao;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.dao.DictDao;
import com.atguigu.dao.HouseDao;
import com.atguigu.entity.House;
import com.atguigu.service.HouseService;
import com.atguigu.vo.HouseQueryVo;
import com.atguigu.vo.HouseVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

@Service(interfaceClass = HouseService.class )
@Transactional
public class HouseServiceImpl extends BaseServiceImpl<House> implements HouseService {
    @Autowired
    private HouseDao houseDao;
    @Autowired
    private DictDao dictDao;

    @Override
    protected BaseDao<House> getEntityDao() {
        return houseDao;
    }

    @Override
    public House getById(Serializable id) {
        House house = super.getById(id);
        //houseTypeId---houseTypeName
        String houseTypeName = this.dictDao.getNameById(house.getHouseTypeId());
        house.setHouseTypeName(houseTypeName);
        //floorId---floorName
        String floorName = this.dictDao.getNameById(house.getFloorId());
        house.setFloorName(floorName);
        //buildStructureId---buildStructureName
        String buildStructureName = this.dictDao.getNameById(house.getBuildStructureId());
        house.setBuildStructureName(buildStructureName);
        //directionId---directionName
        String directionName = this.dictDao.getNameById(house.getDirectionId());
        house.setDirectionName(directionName);
        //decorationId---decorationName
        String decorationName = this.dictDao.getNameById(house.getDecorationId());
        house.setDecorationName(decorationName);
        //houseUseId---houseUseName
        String houseUseName = this.dictDao.getNameById(house.getHouseUseId());
        house.setHouseUseName(houseUseName);
        return house;
    }

    @Override
    public PageInfo<HouseVo> findListPage(int pageNum, int pageSize, HouseQueryVo houseQueryVo) {
        PageHelper.startPage(pageNum,pageSize);
         Page<HouseVo> page=houseDao.findListPage(houseQueryVo);
        List<HouseVo> houseVoList = page.getResult();

        for(HouseVo  houseVo : houseVoList){
            String houseTypeName = dictDao.getNameById(houseVo.getId());
            houseVo.setHouseTypeName(houseTypeName);
            String floorName = this.dictDao.getNameById(houseVo.getFloorId());
            houseVo.setFloorName(floorName);
            String directionName = this.dictDao.getNameById(houseVo.getDirectionId());
            houseVo.setDirectionName(directionName);


        }

        return new PageInfo<>(page.getResult());
    }
}
